package com.wzcl.app.controller.student;

import com.wzcl.app.dao.model.SendPath;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.SendPathDto;
import com.wzcl.app.model.vo.SendPathVo;
import com.wzcl.app.service.SendPathService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/sendPath")
@Api(value = "收货地址管理controller", tags = {"收货地址管理接口"})
public class SendPathController{

    @Resource
    private SendPathService service;

    @ApiOperation("新增收货地址")
    @PostMapping("/add")
    public RVO<SendPathVo> addSendPath(@RequestBody @Valid SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        SendPath sendPath = service.addSendPath(sendPathDto);
        return RVO.success(SendPathVo.create(sendPath));
    }

    @ApiOperation("删除收货地址")
    @PostMapping("/del")
    public RVO<SendPathVo> delSendPath(@RequestBody SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        SendPath sendPath = service.delSendPath(sendPathDto);
        return RVO.success(SendPathVo.create(sendPath));
    }


    @ApiOperation("修改收货地址")
    @PostMapping("/update")
    public RVO<SendPathVo> updateSendPath(@RequestBody @Valid SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        SendPath sendPath = service.updateSendPath(sendPathDto);
        return RVO.success(SendPathVo.create(sendPath));
    }

    @ApiOperation("查看收货地址")
    @PostMapping("/get")
    public RVO<SendPathVo> getSendPath(@RequestBody SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        SendPath sendPath = service.getSendPathById(sendPathDto.getId());
        if(sendPath != null && sendPath.getUserPhone().equals(sendPathDto.getUserPhone())) {
            return RVO.success(SendPathVo.create(sendPath));
        }
        return RVO.success(null);
    }

    @ApiOperation("查看收货地址List")
    @PostMapping("/getList")
    public RVO<List<SendPathVo>> getSendPathList(@RequestBody SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        List<SendPath> list = service.getSendPathList(sendPathDto);
        if(list != null && list.size()>0){
            return RVO.success(list.stream().map(SendPathVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @ApiOperation("查看收货地址Page")
    @PostMapping("/getPage")
    public RVO<List<SendPathVo>> getSendPathPage(@RequestBody SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        MyPage<SendPath> Page = service.getSendPathPage(sendPathDto);
        if(Page != null && Page.getContent().size()>0){
            return RVO.success(Page.getContent().stream().map(SendPathVo::create).collect(Collectors.toList()));
        }
        return RVO.success(null);
    }

    @ApiOperation("设置默认地址")
    @PostMapping("/setDefault")
    public RVO<SendPathVo> setDefaultPath(@RequestBody SendPathDto sendPathDto){
        sendPathDto.setUserPhone(SecurityContextHolder.getContext().getAuthentication().getName());
        SendPath sendPath = service.setDefaultPath(sendPathDto);
        return RVO.success(SendPathVo.create(sendPath));
    }
}
